home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 101 / CD-ROM 101.iso / compl / maya5ple / Install_MayaPLE5_English.exe / Maya / Data1.cab / lineIntersection.mel < prev    next >
Encoding:
Text File  |  2003-07-17  |  3.3 KB  |  102 lines

  1. // Copyright (C) 1997-2002 Alias|Wavefront,
  2. // a division of Silicon Graphics Limited.
  3. //
  4. // The information in this file is provided for the exclusive use of the
  5. // licensees of Alias|Wavefront.  Such users have the right to use, modify,
  6. // and incorporate this code into other products for purposes authorized
  7. // by the Alias|Wavefront license agreement, without fee.
  8. //
  9. // ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  10. // INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  11. // EVENT SHALL ALIAS|WAVEFRONT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  12. // CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  13. // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  14. // TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. // PERFORMANCE OF THIS SOFTWARE.
  16. //
  17. //
  18. //  Alias|Wavefront Script File
  19. //  MODIFY THIS AT YOUR OWN RISK
  20. //
  21. //  Creation Date:  July 30, 1997
  22. //<doc>
  23. //<name lineIntersection>
  24. //<owner "Alias|Wavefront Unsupported">
  25. //
  26. //<synopsis>
  27. //        float [] lineIntersection(float $p1[], float $v1[],
  28. //            float $p2[], float $v2[])
  29. //
  30. //<description>
  31. //        Returns the intersection point of two 3D lines.  Each line is
  32. //        described by a 3D point and direction.
  33. //
  34. //<flags>
  35. //        float[]    $p1    Starting point of first line
  36. //        float[]    $v1    Vector direction of first line
  37. //        float[]    $p2    Starting point of second line
  38. //        float[]    $v2    Vector direction of second line
  39. //
  40. //<returns>
  41. //        float[] : Intersection point of the lines
  42. //
  43. //</doc>
  44. //
  45. global proc float [] lineIntersection( 
  46.     float $p1[], float $v1[],    // 1st line, described by a point + direction
  47.     float $p2[], float $v2[])    // 2nd line, described by a point + direction
  48. {
  49.     if( size($p1) != 3 )
  50.         warning("lineIntersection(): You must give a float array of 3 values as the 1st argument\n");
  51.     if( size($v1) != 3 )
  52.         warning("lineIntersection(): You must give a float array of 3 values as the 2nd argument\n");
  53.     if( size($p2) != 3 )
  54.         warning("lineIntersection(): You must give a float array of 3 values as the 3rd argument\n");
  55.     if( size($v2) != 3 )
  56.         warning("lineIntersection(): You must give a float array of 3 values as the 4th argument\n");
  57.  
  58.     // Ad = A + (B - A).(a - a.b b)/(1 - a.b)2
  59.     // Bd = B + (A - B).(b - a.b a)/(1 - a.b)2
  60.  
  61.     float $A[3]; float $a[3];
  62.     float $B[3]; float $b[3];
  63.     float $C[3]; float $D[3];
  64.     float $Ad[3]; float $Bd[3];
  65.     copyArray( $p1, $A, 3 ); copyArray( $p2, $B, 3 );
  66.     copyArray( $p1, $A, 3 ); copyArray( $p2, $B, 3 );
  67.     copyArray( $v1, $a, 3 ); copyArray( $v2, $b, 3 );
  68.         
  69.     float $c = dotProduct( $v1, $v2, 0 );
  70.     float $h = 1.0 - $c * $c;
  71.  
  72.     $D[0]=$a[0]-$c*$b[0]; 
  73.     $D[1]=$a[1]-$c*$b[1]; 
  74.     $D[2]=$a[2]-$c*$b[2];
  75.     $C[0]=$B[0]-$A[0]; 
  76.     $C[1]=$B[1]-$A[1]; 
  77.     $C[2]=$B[2]-$A[2];
  78.     float $s = dotProduct( $C, $D, 0 ) / $h;
  79.  
  80.     $D[0]=$b[0]-$c*$a[0]; 
  81.     $D[1]=$b[1]-$c*$a[1]; 
  82.     $D[2]=$b[2]-$c*$a[2];
  83.     $C[0]=$A[0]-$B[0]; 
  84.     $C[1]=$A[1]-$B[1]; 
  85.     $C[2]=$A[2]-$B[2];
  86.     float $t = dotProduct( $C, $D, 0 ) / $h;
  87.  
  88.     $Ad[0]=$A[0]+$s*$a[0]; 
  89.     $Ad[1]=$A[1]+$s*$a[1]; 
  90.     $Ad[2]=$A[2]+$s*$a[2];
  91.     $Bd[0]=$B[0]+$t*$b[0]; 
  92.     $Bd[1]=$B[1]+$t*$b[1]; 
  93.     $Bd[2]=$B[2]+$t*$b[2];
  94.  
  95.     // Intersection of the two lines is the mid point between Ad and Bd
  96.     //
  97.     float $intersection[3];
  98.     $intersection = midPoint2Pts( $Ad, $Bd );
  99.  
  100.     return $intersection;
  101. }
  102.